package cn.airfei.aircore.testcore.services.domainService.impl;

import cn.airfei.aircore.core.properties.AirCoreProperty;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.airfei.aircore.core.utils.QueryParamUtil;
import cn.airfei.aircore.testcore.storages.entity.Goods;
import cn.airfei.aircore.testcore.infrastructure.mapper.GoodsMapper;
import cn.airfei.aircore.testcore.services.domainService.GoodsDomainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.airfei.aircore.testcore.storages.dto.GoodsDto;
import cn.airfei.aircore.testcore.storages.vo.GoodsVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.math.NumberUtils;
import cn.airfei.aircore.core.constants.AirStatusConstants;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import cn.airfei.aircore.core.storage.PageParam;

/**
 * <p>
 * 商品
 * </p>
 *
 * @author air
 * @since 2022-04-25
 */
@Service
public class GoodsDomainServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsDomainService {
    @Resource
    private AirCoreProperty airCoreProperty;

    @Resource
    private GoodsMapper goodsMapper;


    /**
     *   
     * @param goodsDto
     * @param pageParam
     * @return
     */
    @Override
    public Page<GoodsVo> index(GoodsDto goodsDto,PageParam pageParam) {
        Page<GoodsDto> page=new Page<>(pageParam.getPageCurrent(),pageParam.getPageSize());
        return goodsMapper.index(page,goodsDto,pageParam);
    }

    /**
     * 分页查询单表数据
     *
     * @param map
     * @return
     */
    @Override
    public Page<Goods> singleTablePage(Map<String, Object> map) {
        QueryWrapper<Goods> queryWrapper= QueryParamUtil.getEntityWrapper(map,Goods.class);
        queryWrapper.lambda().orderByDesc(Goods::getId);
        Page<Goods> page=new Page<>(Long.parseLong(String.valueOf(map.get("pageCurrent"))),Long.parseLong(String.valueOf(map.get("pageSize"))));
        return goodsMapper.selectPage(page,queryWrapper);
    }

    /**
     * 显示单条数据
     *
     * @param id
     * @return
     */
    @Override
    public Goods show(String id){
        QueryWrapper<Goods> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return goodsMapper.selectOne(queryWrapper);
    }

    /**
     * 删除（销毁表数据）,逻辑删除,设置status为-1
     *
     * @param ids
     * @return
     */
    @Override
    public Integer destroy(String ids){
        List<Long> idLongList = Arrays.asList(ids.split(",")).stream()
            .map(s -> NumberUtils.createLong(s.trim())).collect(Collectors.toList());

        QueryWrapper<Goods> queryWrapper=new QueryWrapper();
        queryWrapper.in("id",idLongList);
        Goods goods=new Goods();
        goods.setStatus(AirStatusConstants.delete);
        return goodsMapper.update(goods,queryWrapper);
    }

    /**
     * 更新表数据
     *
     * @param goods
     * @return
     */
    @Override
    public Integer update(Goods goods){
        return goodsMapper.updateById(goods);
    }

    /**
     * 保存表数据
     *
     * @param goods
     * @return
     */
    @Override
    public Integer store(Goods goods){
        return goodsMapper.insert(goods);
    }

    /**
     * 设置状态，1:正常,2:禁用/待审核，-1:已删除
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public Integer setStatus(Integer id, Integer status) {
        Goods goods=new Goods();
        goods.setId(id);
        goods.setStatus(status);
        return goodsMapper.updateById(goods);
    }

    /**
     * get all resource in storage
     * @return
     */
    @Override
    public List<Goods> getAllGoods() {
        QueryWrapper<Goods> queryWrapper=new QueryWrapper();
        queryWrapper.lambda()
            .orderByDesc(Goods::getId)
            .last("limit " + airCoreProperty.getMaxSelect());
        return goodsMapper.selectList(queryWrapper);
    }

    /**
     * get list in single table
     *
     * @param map
     * @return
     */
    @Override
    public List<Goods> getSingleTableList(Map map) {
        QueryWrapper<Goods> queryWrapper = QueryParamUtil.getEntityWrapper(map, Goods.class);
        queryWrapper.lambda().orderByDesc(Goods::getId).last("limit " + airCoreProperty.getMaxSelect());
        return goodsMapper.selectList(queryWrapper);
    }

}
